<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="StyleSheet" type="text/css" href="{%$templates%}/admin.css">
<link type="text/css" rel="stylesheet" href="{%$library%}/yui/fonts.css">
<link type="text/css" rel="stylesheet" href="{%$library%}/yui/treeview.css">

<script type="text/javascript" src="{%$library%}/yui/yahoo.js"></script>
<script type="text/javascript" src="{%$library%}/yui/event.js" ></script>
<script type="text/javascript" src="{%$library%}/yui/treeview.js" ></script>
<script type="text/javascript" src="{%$library%}/fckeditor/fckeditor.js"></script>
<script type="text/javascript" src="{%$library%}/jquery.js"></script>

<script language="javascript" type="text/javascript">
<!--
var tree;
var editor_exist = false;

function MeccanoTree()
{
   if(document.getElementById('lang'))
   {
      var lang = document.getElementById('lang').value;
   } else
   {
      var lang = false;
   } 
   $.getJSON('content.php', {'lang': lang, 'e':7}, function(data)
   {
      if(data)
      {
         tree = new YAHOO.widget.TreeView("_tree_");
         var root = tree.getRoot();
         RenderTree(data, root);
         tree.subscribe("labelClick", function(node)
         {
            LoadContent(node.data.id);
         });
         tree.expandAll();

         tree.draw();
      }
   });
}

function LoadContent(id)
{
   var lang = document.getElementById('lang').value;
   $.getJSON('content.php', {'lang': lang, 'id':id, 'e':6}, function(data)
   {
      if(data != 0)
      {
         document.getElementById('id').value = data.id;
         document.getElementById('title').value = data.title;
         document.getElementById('metadescr').value = data.metadescr;
         document.getElementById('metawords').value = data.metawords;
         document.getElementById('editor').value = data.content;
      } else
      {
         document.getElementById('id').value = id;
         document.getElementById('title').value = '';
         document.getElementById('metadescr').value = '';
         document.getElementById('metawords').value = '';
      }

      if(editor_exist)
      {
         var oEditor = FCKeditorAPI.GetInstance('editor');
         var cont = '';
         if(data != 0) cont = data.content;
         oEditor.SetHTML(cont);
      } else
      {
         var sBasePath = '/lib/fckeditor/';
         var oFCK = new FCKeditor('editor');
         oFCK.BasePath = sBasePath;
         oFCK.Config['CustomConfigurationsPath'] = sBasePath + 'm_config.js';
         oFCK.ToolbarSet = 'Advanced';
         oFCK.Height = 700;
         oFCK.Width  = 650;
         oFCK.ReplaceTextarea();
         editor_exist = true;
      }
   });
   display_editor(true);
}

function do_save()
{
   id = document.getElementById('id').value;
   lang = document.getElementById('lang').value;
   title = document.getElementById('title').value;
   descr = document.getElementById('metadesc').value;
   meta = document.getElementById('metawords').value;
   var oEditor = FCKeditorAPI.GetInstance('editor') ;
   var html = oEditor.GetXHTML();

   $.ajax({
      type: "POST",
      url: "/admin/content.php",
      data: "e=8&id="+id+"&lang="+lang+"&title="+title+"&descr="+descr+"&meta="+meta+"&html="+html,
      dataType: "json",
      success: function(data){
         if(data == 1)
         {
            display_make(true);
         } else
         {
            display_error(true);
         }
      }
   });
}

function RenderTree(data, p)
{
   var i = 0;
   if(data[0].id > 0)
	{
      for (var i in data)
		{
         var tmpNode = new YAHOO.widget.TextNode({label:data[i].title, id:data[i].id}, p, false);

         if(data[i].branch)
         {
            RenderTree(data[i].branch, tmpNode);
         }
         i++;
      }
   }
}

function display_editor(sw)
{
   var c = document.getElementById('content');
   if(sw)
   {
      c.style.visibility = 'visible';
   } else
   {
      c.style.visibility = 'hidden';
   }
}

YAHOO.util.Event.onDOMReady(MeccanoTree);
</script>
</head>

<body bgcolor='#ffffff' marginheight='0' topmargin='0'>

<table border='0' cellpadding='0' cellspacing='1' width='100%' class='text'>
   <tr>
      <td width='250px' style='padding-left:20px; padding-top: 10px; border-right: solid 1px #ffcc44' valign='top'>
         {%if count($langs) > 1%}
            Язык: &nbsp;
            <select name='lang' class='form_f' style='padding:1px;' id='lang' onchange='MeccanoTree(getElementById("lang").value);'>
               {%html_options values=$langs output=$langs selected=$lang%}
            </select>
            <hr size='1' noshade width='100%'>
         {%else%}
            <input type='hidden' name='lang' id='lang' value='{%$langs[0]%}'>
         {%/if%}

         <div id="_tree_"></div>
      </td>

      <td align='center' valign='top' style='padding-left: 20px; padding-top: 10px'>

         <div id='content' style='visibility:hidden;'>
            <form name="cont" action='content.php?e=8' method='POST'>
               <input type='hidden' name='id' id='id' value='{%$id%}'>

               <table border='0' cellpadding='3' cellspacing='1' width='95%' class='text'>

                  <tr>
                     <td align='right'>Заголовок страницы: &nbsp;</td>
                     <td align='left'> &nbsp;
                        <input type='text' name='cont_title' class='form_f' style='width: 300px' id="title">
                     </td>
                  </tr>

                  <tr>
                     <td align='right' valign='top'>Описание (META description): &nbsp;</td>
                     <td align='left'> &nbsp;
                        <textarea name='cont_description' rows='3' cols='50' id='metadescr'></textarea>
                     </td>
                  </tr>

                  <tr>
                     <td align='right' valign='top'>Ключевые слова (META words): &nbsp;</td>
                     <td align='left'> &nbsp;
                        <textarea name='cont_metawords' rows='2' cols='50' id='metawords'></textarea>
                     </td>
                  </tr>

                  <tr>
                     <td align='left' colspan='2' align='center' valign='top'>
                        Page:<br>
                        <textarea name='cont_content' rows='20' cols=60' id='editor'></textarea>
                     </td>
                  </tr>
               </table>
            </form>
         </div>
      </td>
   </tr>
</table>
<div id='debug'></div>
</body>
</html>